﻿'=============================
'【窗体名称】参数管理
'【作者】罗邓
'【功能说明】
'   1. 左侧显示 CONFIG 参数列表；
'   2. 用户选中某一参数后，右侧显示详细信息；
'   3. 用户可修改“参数值”，点击保存后更新数据库；
'   4. 点击“退出”按钮可关闭窗体。
'=============================
Imports System.Data.SqlClient

Public Class 参数管理

    ' 定义数据库连接字符串（使用公共连接方法）
    Private connectionString As String = DbConnectionHelper.GetConnectString()

    '==============================
    '【1】窗体加载事件：加载参数列表
    '==============================
    Private Sub 参数管理_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadConfigData()
        ClearRightPanel()
    End Sub

    '==============================
    '【2】加载参数列表（左侧 DataGridView）
    '==============================
    Private Sub LoadConfigData()
        Try
            Using conn As New SqlConnection(connectionString)
                conn.Open()
                Dim sql As String = "SELECT ID, CSNR AS 参数内容, CSZ AS 参数值, SCXGSJ AS 修改时间 FROM CONFIG ORDER BY ID"
                Dim da As New SqlDataAdapter(sql, conn)
                Dim dt As New DataTable()
                da.Fill(dt)

                dgvConfig.DataSource = dt
                dgvConfig.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill

                ' 设置列标题
                dgvConfig.Columns("ID").HeaderText = "编号"
                dgvConfig.Columns("参数内容").HeaderText = "参数内容"
                dgvConfig.Columns("参数值").HeaderText = "参数值"
                dgvConfig.Columns("修改时间").HeaderText = "上次修改时间"

                ' 禁止用户直接修改表格内容
                dgvConfig.ReadOnly = True
                dgvConfig.SelectionMode = DataGridViewSelectionMode.FullRowSelect
                dgvConfig.MultiSelect = False
            End Using
        Catch ex As Exception
            MessageBox.Show("加载参数列表时出错：" & ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    '==============================
    '【3】选中某一参数时，将数据同步到右侧输入框
    '==============================
    Private Sub dgvConfig_SelectionChanged(sender As Object, e As EventArgs) Handles dgvConfig.SelectionChanged
        If dgvConfig.SelectedRows.Count > 0 Then
            Dim row As DataGridViewRow = dgvConfig.SelectedRows(0)
            txtID.Text = row.Cells("ID").Value.ToString()
            txtCSNR.Text = row.Cells("参数内容").Value.ToString()
            txtCSZ.Text = row.Cells("参数值").Value.ToString()
            ' txtXGSJ.Text = row.Cells("修改时间").Value.ToString()
        End If
    End Sub

    '==============================
    '【4】清空右侧输入框
    '==============================
    Private Sub ClearRightPanel()
        txtID.Text = ""
        txtCSNR.Text = ""
        txtCSZ.Text = ""
        '  txtXGSJ.Text = ""
    End Sub

    '==============================
    '【5】保存按钮事件：更新数据库中的参数值
    '==============================
    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        ' 校验是否选择了参数
        If txtID.Text.Trim() = "" Then
            MessageBox.Show("请先选择要修改的参数！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Return
        End If

        Try
            Using conn As New SqlConnection(connectionString)
                conn.Open()
                Dim sql As String = "UPDATE CONFIG SET CSZ = @CSZ, SCXGSJ = GETDATE() WHERE ID = @ID"
                Using cmd As New SqlCommand(sql, conn)
                    cmd.Parameters.AddWithValue("@CSZ", txtCSZ.Text.Trim())
                    cmd.Parameters.AddWithValue("@ID", Convert.ToInt32(txtID.Text))
                    cmd.ExecuteNonQuery()
                End Using
            End Using

            MessageBox.Show("参数更新成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
            LoadConfigData() ' 重新加载左侧数据
        Catch ex As Exception
            MessageBox.Show("保存参数时出错：" & ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    '==============================
    '【6】退出按钮事件：关闭窗体
    '==============================
    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnClose.Click
        Me.Close() '关闭当前窗体
    End Sub

    Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
        LoadConfigData()
    End Sub
End Class
